﻿using System;
using System.DHTML;
using Sys;
using Sys.UI;
using Sys.Mvc;

using JQuerySharp;
using VietPublisher.Scripts.Resources;

static public class SC_Menu
{
    public static string ControllerUrl = "Menu";

    #region 메뉴관리 - Menu Mgt

    public static void ReloadMenuList(string menuId)
    {
        Utils.Navigate(SC_Common.BasePath + ControllerUrl + (menuId == "" ? "/Index" : ("/Index?menuid=" + menuId)));
    }

    /// <summary>
    /// ON adding new menu
    /// </summary>
    public static void AddNewMenu()
    {
        string upMenuId = Utils.GetInputValue("MenuID");
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/Insert?upMenuId={0}", upMenuId);
        UIHelper.LoadAjax(href, "divEditMenu", null, null);
    }

    /// <summary>
    /// Save and insert
    /// </summary>
    /// <param name="form"></param>
    public static void SaveMenu(Dictionary form)
    {
        string menuId = Utils.GetInputValue("MenuID");
        bool isAdded = menuId == "0";

        string upMenuId = Utils.GetInputValue("UpMenuID");
        string menuName = Utils.GetInputValue("MenuName");

        if (isAdded)
        {
            UIHelper.SubmitAjax(form, "Post", null,
            new MvcCallback(delegate(AjaxContext context)
            {
                Script.Alert(Common.OperationSuccess);
                menuId = context.request.Executor.ResponseData;
                ReloadMenuList(menuId);
            }), null);
        }
        else
        {
            UIHelper.SubmitAjax(form, "Post", null,
            new MvcCallback(delegate(AjaxContext context)
            {
                if (context.request.Executor.ResponseData == "")
                {
                    Script.Alert(Common.OperationSuccess);
                    ReloadMenuList(menuId);
                }
                else
                {
                    Script.Alert(Common.SaveMessageFail);
                }

            }), null);
        }


    }

    /// <summary>
    /// Delete menu
    /// </summary>
    public static void DeleteMenu()
    {
        string menuId = Utils.GetInputValue("MenuID");
        if (menuId == "0")
        {
            return;
        }

        string upMenuId = Utils.GetInputValue("UpMenuID");

        if (!Script.Confirm(Common.DeleteConfirm))
        {
            return;
        }

        string href = SC_Common.BasePath + ControllerUrl + string.Format("/Delete?menuId={0}", menuId);
        UIHelper.LoadAjax(href, null, new MvcCallback(delegate(AjaxContext context)
            {
                Script.Alert(Common.DeleteMessageComplete);
                ReloadMenuList(menuId);
            }), null);

    }

    /// <summary>
    /// Select menu
    /// </summary>
    /// <param name="menuId"></param>
    public static void SelectMenu(string menuId)
    {
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/Details?menuId={0}&dt={1}", menuId, new DateTime());
        UIHelper.LoadAjax(href, "divEditMenu",
            new MvcCallback(delegate(AjaxContext context)
                {
                    //SC_Menu.InitEditMenuForm();
                })
            , null);
    }

    /// <summary>
    /// Apply
    /// </summary>
    public static void InitEditMenuForm()
    {
        Utils.ApplyNumericTextBox(new string[] { "MenuOrder", "PopupWidth", "PopupHeight" }, false, false);
    }

    #endregion

    #region Menu Navigation

    /// <summary>
    /// Navigate Top Menu
    /// </summary>
    /// <param name="menuId"></param>
    /// <param name="menuUrl"></param>
    public static void NavigateMenu(string menuId, string menuUrl)
    {
        string href = SC_Common.BasePath + ControllerUrl + "/NavigateTopMenu?menuid=" + menuId + "&menuurl=" + menuUrl;

        UIHelper.LoadAjax(href, null, new MvcCallback(delegate(AjaxContext context)
            {
                Script.Literal("document.location.href = {0};", menuUrl);
            }), null);
    }

    /// <summary>
    /// Render
    /// </summary>
    /// <param name="divTargetId"></param>
    /// <param name="renderUrl"></param>
    public static void RenderCustomChildTree(string divTargetId, string renderUrl)
    {
        UIHelper.LoadAjaxNullEvent(renderUrl, divTargetId, null);
    }

    #endregion

    #region Menu Permission

    public static void OnClickTab(string tabName)
    {
        Utils.SetInputValue("Tab1_or_Tab2", tabName);
        TableElement[] parent = (TableElement[])(object)JQueryFactory.JQuery("#tabTable");
        TableElement tableParent = (TableElement)parent[0];
        DOMElement row = (DOMElement)tableParent.Rows[0];
        


        for (int i = 0; i < row.ChildNodes.Length; i++)
        {
           
            DivElement div = null;
            if (row.ChildNodes[i].ID != null)
            {
                div = (DivElement)Document.GetElementById(row.ChildNodes[i].ID.Substr(2));
            }
            if (div != null)
            {
                div.Style.Display = "none";
                row.ChildNodes[i].Style.BackgroundImage = row.ChildNodes[i].Style.BackgroundImage.Replace("tab_bg2.jpg", "tab_bg1.jpg");
            }

            if (row.ChildNodes[i].ID == "bt" + tabName)
            {
                row.ChildNodes[i].Style.BackgroundImage = row.ChildNodes[i].Style.BackgroundImage.Replace("tab_bg1.jpg", "tab_bg2.jpg");

                DivElement divSelected = (DivElement)Document.GetElementById(tabName);
                divSelected.Style.Display = "block";

            }


        }

    }
    #region tab1
    public static void selectMenu_Permission(string menuId)
    {
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/Details_Permission?menuId={0}", menuId + "&dt=" + new DateTime());
        UIHelper.LoadAjax(href, "divEditMenuPermission",
            new MvcCallback(delegate(AjaxContext context)
            {
                //SC_Menu.InitEditMenuForm();
            })
            , null);
    }
    public static void SetGroupToList()
    {

        jQuery query = JQueryFactory.JQuery("#ListOfGroup option:selected");
        //jQuery query = JQueryFactory.JQuery("#ListOfGroup option[selected='selected']");
        InputElement[] list = (InputElement[])(object)query;

        jQuery query2 = JQueryFactory.JQuery("#lstMyMenus option");
        jQuery query3 = JQueryFactory.JQuery("#lstMyMenus");
        InputElement[] list2 = (InputElement[])(object)query2;


        for (int i = 0; i < list.Length; i++)
        {

            int numberTrung = 0;
            for (int j = 0; j < list2.Length; j++)
            {

                if (list2[j].Value == list[i].Value)
                {
                    numberTrung++;
                }
            }

            if (numberTrung == 0)
            {
                //chuoi += list[i].InnerText + "," + list[i].Value + "$";
                query3.Append("<option value=" + list[i].Value + ">" + list[i].InnerText + "</option>");
            }

        }

    }
    public static void removeGroupFromList()
    {

        jQuery query = JQueryFactory.JQuery("#lstMyMenus option");
        InputElement[] list = (InputElement[])(object)query;

        jQuery query2 = JQueryFactory.JQuery("#lstMyMenus option:selected");
        InputElement[] list2 = (InputElement[])(object)query2;
        if (list2 != null)
        {
            for (int i = 0; i < list2.Length; i++)
            {
                JQueryFactory.JQuery("#lstMyMenus option[value=" + list2[i].Value + "]").ReplaceWith("");
            }
        }
    }
    public static void saveGroupPermissionToMenu()
    {
        string MenuID = Utils.GetInputValue("IdOfMenu");
        jQuery query = JQueryFactory.JQuery("#lstMyMenus option");
        InputElement[] list = (InputElement[])(object)query;
        string Group = "";

        for (int i = 0; i < list.Length; i++)
        {
            Group += list[i].Value + "," + list[i].InnerText + "$";
        }

        string href = SC_Common.BasePath + ControllerUrl + string.Format("/saveGroupPermissionToMenu?menuId={0}", MenuID + "&Group=" + Group + "&dt=" + new DateTime());
        UIHelper.LoadAjax(href, "DivNone",
            new MvcCallback(delegate(AjaxContext context)
            {
                Script.Alert(Common.OperationSuccess);
            })
            , null);
    }
    #endregion
    #region tab2
    public static void setMenuToList(string selectedGroupDepts)
    {
        jQuery query2 = JQueryFactory.JQuery("#lstMyMenus2 option");
        jQuery query3 = JQueryFactory.JQuery("#lstMyMenus2");
        InputElement[] list2 = (InputElement[])(object)query2;

        string[] arr = selectedGroupDepts.Split('\n');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].Trim() == "")
            {
                continue;
            }
            string[] list = arr[i].Split('|');
            string value = list[0].ToString();
            string fullpath = list[1].ToString().Substring(0, list[1].Length - 2);
            int numberTrung = 0;
            for (int j = 0; j < list2.Length; j++)
            {
                if (list2[j].Value == value)
                {
                    numberTrung++;
                }
            }
            if (numberTrung == 0)
            {
                query3.Append("<option value=" + value + ">" + fullpath + "</option>");
            }
        }
    }
    public static void selectGroup_Permission(string GroupId,string GroupName)
    {
        Utils.SetInputValue("IdOfGroup", GroupId);
        Utils.SetInputValue("NameOfGroup", GroupName.Trim());
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/Details_Permission_tab2?GroupId={0}", GroupId + "&dt=" + new DateTime());
        UIHelper.LoadAjax(href, "divEditMenuPermission2",
            new MvcCallback(delegate(AjaxContext context)
            {
                //SC_Menu.InitEditMenuForm();
            })
            , null);
    }
    public static void removeGroupFromList2()
    {

        jQuery query = JQueryFactory.JQuery("#lstMyMenus2 option");
        InputElement[] list = (InputElement[])(object)query;

        jQuery query2 = JQueryFactory.JQuery("#lstMyMenus2 option:selected");
        //jQuery query2 = JQueryFactory.JQuery("#lstMyMenus2 option[selected='selected']");
        InputElement[] list2 = (InputElement[])(object)query2;
        if (list2 != null)
        {
            for (int i = 0; i < list2.Length; i++)
            {
                JQueryFactory.JQuery("#lstMyMenus2 option[value=" + list2[i].Value + "]").ReplaceWith("");
            }
        }
    }
    public static void saveMenuPermissionToGroup()
    {
        if (Utils.GetInputValue("IdOfGroup") == null || Utils.GetInputValue("IdOfGroup") == "")
        {
            return;
        }
        else
        {
            string GroupID = Utils.GetInputValue("IdOfGroup");
            jQuery query = JQueryFactory.JQuery("#lstMyMenus2 option");
            InputElement[] list = (InputElement[])(object)query;
            string Menu = "";

            for (int i = 0; i < list.Length; i++)
            {
                Menu += list[i].Value + ",";
            }
            string href = SC_Common.BasePath + ControllerUrl + string.Format("/saveMenuPermissionToGroup?GroupID={0}", GroupID + "&Menu=" + Menu + "&dt=" + new DateTime());
            UIHelper.LoadAjax(href, "DivNone",
                new MvcCallback(delegate(AjaxContext context)
                {
                    Script.Alert(Common.OperationSuccess);
                })
                , null);

        }

    }
    #endregion
}
    #endregion

